package com.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.pageResult.PageResult;
import com.pojo.CheckGroup;
import com.query.QueryPageBean;
import com.result.Result;
import com.service.CheckGroupService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> list = checkGroupService.queryAll();
           return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    //查询检查组（分页+条件）
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult result = checkGroupService.selectByQueryString(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /*删除*/
    @PostMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        checkGroupService.delete(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }


    //添加
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
    }

    //根据id查询
    @RequestMapping(value = "/queryById")
    public Result queryById(Integer id){
        try {
            Integer[] ids = checkGroupService.queryByCheckItemId(id);
            CheckGroup checkGroup = checkGroupService.queryById(id);
            Map map=new HashMap();
            map.put("checkGroup",checkGroup);
            map.put("ids",ids);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        try {
            checkGroupService.update(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
    }

}
